import cv2
import base64
from adduser import Ui_Dialog
from PyQt5.QtWidgets import QDialog, QMessageBox
from PyQt5.QtCore import QTimer
from camera import camera


class adduserWindow(Ui_Dialog, QDialog):
    def __init__(self, list, parent=None):
        super(adduserWindow, self).__init__(parent)
        self.setupUi(self)  # 创建界面内容
        self.label.setScaledContents(True) # 自适应
        self.camera_data = camera()
        self.time = QTimer()
        self.time.start(50)
        self.time.timeout.connect(self.show_camera)
        self.pushButton.clicked.connect(self.get_cameradata)
        # 添加班级到comboBox中
        self.show_list(list)
        self.pushButton_2.clicked.connect(self.get_data_close)
        self.pushButton_3.clicked.connect(self.window_close)

    def show_camera(self):
        # 获取摄像头转换数据
        img = self.camera_data.camera_toPic()
        # 显示画面
        self.label.setPixmap(img)


    def get_cameradata(self):
        # 摄像头获取画面
        camera_img = self.camera_data.read_camera()
        # 把摄像头画面转换成图片
        _, enc = cv2.imencode('.jpg', camera_img)
        # 设置为base64编码格式
        self.base64_image = base64.b64encode(enc.tobytes())
        self.time.stop()
        self.camera_data.camera_close()

    def show_list(self, list):
        for i in list:
            self.comboBox.addItem(i)


    def get_data_close(self):
        self.user_id = self.lineEdit.text()
        self.user_name = self.lineEdit_2.text()
        self.group_id = self.comboBox.currentText()
        if self.user_name == '' or self.user_id == '':
            QMessageBox.warning(self, "warning", "姓名和学号不能为空！")
            return
        self.accept()


    def window_close(self):
        self.close()
